#include "includes.h"

int main()
{
    string temp = "title ";
    string verTitle = temp + version;
	if (CONSOLEDEBUG)
	{
        verTitle = temp + versiondbg;
	}
	else
	{
        //do nothing
	}
    system(verTitle.c_str());
	//printCon(__DATE__, 0);
	//printCon(__TIME__, 0);
	string scmd;
	system("color 0A");
	printabout();
	Sleep(1000);
	clrscr();
	/*char versionChar[] = version, license[] = lic, maker[] = by;
	printCon(versionChar);
	printCon(license);
	printCon(maker);*/
	info("all commands are lowercase");
    nologin:
    if(!loggedin)
    {
        printCon("successfully logged out\n", 40);
        loggedin = 1;
    }

    if (CONSOLEDEBUG)
    {
        info("DEBUG: SKIPPING LOGON . . .");
        goto validlogin;
    }
    if(BETA)
    {
        printCon("FOR TESTING PURPOSES ONLY\n\n", 25);
    }
	cout << endl << ">";
	cin >> scmd;
	if (scmd != "login")
	{
		warn("login required");
		goto nologin;
	}
	cin >> user;
	if (user != "help") cin >> pass;
	if (user == "chell" && pass == "portal" || user == "cjohnson" && pass == "tier3")
	{
		info("login successful");
		info("for a list of commands, type help");
        string infod = "logged in as: ";
        string passin = infod + user;
        info(passin);
		goto validlogin;
	}
	else if (user == "help")
	{
		info("SYNTAX: login [username] [password]");
		info("if you dont know the user or password, play chamber 17 of portal");
		goto nologin;
	}
	else
	{
		warn("invalid login");
		info("for help logging in, type \"login help\"");
		goto nologin;
	}

	validlogin:
	loggedin = 1;
	cout << endl << ">";
	cin >> scmd;
	validateCommand(scmd);
	if(!rtnValid)
	{
	    string inv = "invalid command: ";
	    string proccmd = inv + scmd;
	    warn(proccmd);
	}
	else if(rtnValid == 255)
	{
	    loggedin = 0;
	    goto nologin;
	}
	else if(rtnValid == 2545)
	{
	    string sel;
	    //char prompt[] = ;
	    printCon("are you sure? (y/n): ", 25);
	    cin >> sel;
	    if(sel == "y")
	    {
	        goto rtn;
	    }
	    else
	    {
	        //do nothing
	    }
	}
	goto validlogin;

	rtn:
	return 1337;
}

void warn(string warning)
{
	//beeps and warns
	string beepwarns = "No";
//	string beepnwarn = beep + warn + warning;

	//cout << "\a" << "#" << warning << endl;
    string ast = "\a#";
    string lb = "\n";
	string info2 = ast + warning + lb;
	const char* test = info2.c_str();
	printCon(test, 18);
}

void info(string info)
{
	//info
	//cout << "@" << info << endl;
	string ast = "@";
	string lb = "\n";
	string info2 = ast + info + lb;
	const char* test = info2.c_str();
	printCon(test, 18);
}

bool validateCommand(string command)
{
    if(command == "clear" || command == "cls")
    {
        clrscr();
        if(CONSOLEDEBUG)
        {
            printCon("DEBUG MODE\n\n", 25);
        }
        if(BETA)
        {
            printCompileDate();
            printCon("FOR TESTING PURPOSES ONLY\n\n", 25);
        }
        rtnValid = 1;
    }
    else if(command == "help")
    {
        printCon( "Available commands are:\n    cls/clear: clears the screen\n    help: displays this text\n    run <command>: runs the specified external command\n    about: shows the version information", 15);
        rtnValid = 1;
    }
    else if(command == "logout")
    {
        rtnValid = 255;
    }
    else if(command == "run")
    {
        char ext[128];
        cin.getline( ext, 128 );
        cout << "running external command: " << ext << endl;
        string start = "start \"\" ";
        string cmmd = start + ext;
        const char* extcmd = cmmd.c_str();
        //cout << extcmd;
        system(extcmd);
        rtnValid = 1;
    }
    else if(command == "about")
    {
        printabout();
        rtnValid = 1;
    }
    else if(command == "exit")
    {
        rtnValid = 2545;
    }
    else
        rtnValid = 0;

    return 1;
}

void printCon(const char *sPtr, int milliseconds)
{
    for ( ; *sPtr != '\0'; sPtr++ )
    {
        cout << *sPtr;
        Sleep(milliseconds);
    }
}

void printabout()
{
    cout << version << lic << by;
}

void printCompileDate()
{
    printCon("This build was compiled on ", 5);
	printCon(__DATE__, 5);
	printCon(" at ", 5);
	printCon(__TIME__, 5);
	printCon("\n", 5);
}

//DO NOT, UNDER ANY CIRCUMSTANCES, EDIT ANYTHING BELOW THIS LINE!
void clrscr()
{
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord = {0, 0};
    DWORD count;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(hStdOut, &csbi);
    FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
    SetConsoleCursorPosition(hStdOut, coord);
}
